package fr.bull.learn.thread.chat;

import org.apache.log4j.Logger;

/**
 * Classe mutuelle au thread
 * @author Rouviere-X
 *
 */
public class Chat {
	    boolean flag = false;
	    private Logger log = Logger.getLogger(getClass());
	    
	    public synchronized void question(String msg) {
	        if (flag) {
	            try {
	                wait();
	            } catch (InterruptedException e) {
	                log.error("Chat interrupt ", e);
	            }
	        }
	        log.info(msg);
	        flag = true;
	        notify();
	    }

	    public synchronized void answer(String msg) {
	        if (!flag) {
	            try {
	                wait();
	            } catch (InterruptedException e) {
	            	log.error("Interrupt Thread ! Do something to terminate properly");
	            }
	        }

	        log.info(msg);
	        flag = false;
	        notify();
	    }
}
